<?php

//查找当前字段
function Search($model, $id, $field) {
    $Data = D($model);
    $result = $Data->where(array('id' => $id))->find();
    return $result[$field];
}

//获取列表第一条信息
function SearchOne($model, $parameter, $val, $order = 'id asc') {
    $Data = D($model);
    $result = $Data->where(array($parameter => $val))->order($order)->find();
    return $result;
}

function SearchAll($model, $parameter, $val, $field) {
    $Data = D($model);
    $result = $Data->where(array($parameter => $val))->find();
    return $result[$field];
}

//查找图册封面
function SearchGallery($model, $cid, $field) {
    $Data = D($model);
    $result = $Data->where(array('cid' => $cid))->order('id desc')->find();
    return $result[$field];
}

//字符截取
function msubstr($str, $start = 0, $length, $charset = "utf-8", $suffix = true) {
    if (function_exists("mb_substr")) {
        $slice = mb_substr($str, $start, $length, $charset);
    } elseif (function_exists('iconv_substr')) {
        $slice = iconv_substr($str, $start, $length, $charset);
        if (false === $slice) {
            $slice = '';
        }
    } else {
        $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
        $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
        $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
        $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
        preg_match_all($re[$charset], $str, $match);
        $slice = join("", array_slice($match[0], $start, $length));
    }
    return $suffix ? $slice . '...' : $slice;
}

//无限分类
function toLevel($cate, $delimiter = '———', $parent_id = 0, $level = 0, $sub = 0) {
    $arr = array();
    foreach ($cate as $v) {
        if ($v['parent_id'] == $parent_id) {
            $v['level'] = $level + 1;
            $v['delimiter'] = str_repeat($delimiter, $level);
            if (hasChild($cate, $v['id'])) {
                $v['sub'] = 1;
            } else {
                $v['sub'] = $sub;
            }
            $arr[] = $v;
            $arr = array_merge($arr, toLevel($cate, $delimiter, $v['id'], $v['level']));
        }
    }
    return $arr;
}

//导航分类
function _getNav($id = 0) {
    $navs = M('Category')->where(array('is_show' => '1', 'status' => '1', 'parent_id' => $id))->order("order_id")->select();
    if ($navs) {
        foreach ($navs as $key => $value) {
            $subNavs = _getNav($value['id']); //如果有子菜单 
            if ($subNavs) {
                $navs[$key]['sub'] = $subNavs;
            }
        }
    }
    return $navs;
}

//传递一个子分类ID返回他的所有父级分类
function getParents($cate, $id) {
    $arr = array();
    foreach ($cate as $v) {
        if ($v['id'] == $id) {
            $arr[] = $v;
            $arr = array_merge(getParents($cate, $v['parent_id']), $arr);
        }
    }
    return $arr;
}

//传递一个子分类ID返回他的同级分类
function getSameCate($cate, $id) {
    $arr = array();
    $self = getSelf($cate, $id);
    if (empty($self)) {
        return $arr;
    }
    foreach ($cate as $v) {
        if ($v['id'] == $self['pid']) {
            $arr[] = $v;
        }
    }
    return $arr;
}

//判断分类是否有子分类,返回false,true
function hasChild($cate, $id) {
    $arr = false;
    foreach ($cate as $v) {
        if ($v['parent_id'] == $id) {
            $arr = true;
            return $arr;
        }
    }
    return $arr;
}

//传递一个父级分类ID返回所有子分类ID
/**
 * @param $cate 全部分类数组
 * @param $pid 父级ID
 * @param $flag 是否包括父级自己的ID，默认不包括
 * */
function getChildsId($cate, $pid, $flag = 0) {
    $arr = array();
    if ($flag) {
        $arr[] = $pid;
    }
    foreach ($cate as $v) {
        if ($v['parent_id'] == $pid) {
            $arr[] = $v['id'];
            $arr = array_merge($arr, getChildsId($cate, $v['id']));
        }
    }
    return $arr;
}

//传递一个父级分类ID返回所有子级分类
function getChilds($cate, $pid) {
    $arr = array();
    foreach ($cate as $v) {
        if ($v['parent_id'] == $pid) {
            $arr[] = $v;
            $arr = array_merge($arr, getChilds($cate, $v['id']));
        }
    }
    return $arr;
}

//传递一个分类ID返回该分类相当信息
function getSelf($cate, $id) {
    $arr = array();
    foreach ($cate as $v) {
        if ($v['id'] == $id) {
            $arr = $v;
            return $arr;
        }
    }
    return $arr;
}

//传递一个分类ID返回该分类相当信息
function getSelfByEName($cate, $ename) {
    $arr = array();
    foreach ($cate as $v) {
        if ($v['ename'] == $ename) {
            $arr = $v;
            return $arr;
        }
    }
    return $arr;
}

/**
 * Discuz加密/解密
 *
 * @param[type]$string[description]
 * @paramstring$operation[description]
 * @paramstring$key[description]
 * @paraminteger$expiry[description]
 * @return[type][description]
 */
function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
    
    return $string;
    $ckey_length = 4;
    $key = md5($key != '' ? $key : C('AUTH_CODE'));
    
    $keya = md5(substr($key, 0, 16));
    $keyb = md5(substr($key, 16, 16));
    $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length) : substr(md5(microtime()), -$ckey_length)) : '';

    $cryptkey = $keya . md5($keya . $keyc);
    $key_length = strlen($cryptkey);

    $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $keyb), 0, 16) . $string;
    $string_length = strlen($string);

    $result = '';
    $box = range(0, 255);

    $rndkey = array();
    for ($i = 0; $i <= 255; $i++) {
        $rndkey[$i] = ord($cryptkey[$i % $key_length]);
    }

    for ($j = $i = 0; $i < 256; $i++) {
        $j = ($j + $box[$i] + $rndkey[$i]) % 256;
        $tmp = $box[$i];
        $box[$i] = $box[$j];
        $box[$j] = $tmp;
    }

    for ($a = $j = $i = 0; $i < $string_length; $i++) {
        $a = ($a + 1) % 256;
        $j = ($j + $box[$a]) % 256;
        $tmp = $box[$a];
        $box[$a] = $box[$j];
        $box[$j] = $tmp;
        $result .= chr(ord($string[$i])^($box[($box[$a] + $box[$j]) % 256]));
    }

    if ($operation == 'DECODE') {
        if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb), 0, 16)) {
            return substr($result, 26);
        } else {
            return '';
        }
    } else {
        return $keyc . str_replace('=', '', base64_encode($result));
    }
}
?>